/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 7, 2009 11:28:00 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 7, 2009 11:28:00 PM
 *
 */
package com.gooex.service.organization.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.c4j.system.web.Constants;
import org.c4j.system.web.context.HttpExecContext;
import org.c4j.system.web.context.impl.SessionContextImpl;

import com.gooex.domain.common.Region;
import com.gooex.domain.organization.User;
import com.gooex.service.common.IRegionManager;
import com.gooex.service.organization.AuthorityContext;
import com.gooex.service.organization.IApplicationSessionContext;
import com.gooex.service.organization.IUserManager;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author Eric Yang
 * @version 1.0
 */
public class ApplicationSessionContextImpl extends SessionContextImpl implements IApplicationSessionContext {

	private static final Log logger = LogFactory.getLog(ApplicationSessionContextImpl.class);

	private IUserManager userManager;

	private IRegionManager regionManager;

	public Region getRegion() {
		String regionId = (String) getSession(Constants.USER_REGION);
		if (regionId != null) {
			return regionManager.getRegion(regionId);
		} else {
			String ipAddr = HttpExecContext.get().getRequestContext().getRequest().getRemoteAddr();
			Region region = regionManager.getRegionByIp(ipAddr);
			if (region == null)
				region = regionManager.getGlobalRegion();
			setSession(Constants.USER_REGION, region.getId());
			return region;
		}
	}

	public User getUser() {
		String account = getAccount();
		return getUser(account);
	}

	private User getUser(String account) {
		if (account == null || account.length() == 0) {
			return null;
		}
		return userManager.getUserByAccount(account);
	}

	public void clearContext(String userId) {
		super.clearContext(userId);
	}

	public Object getSession(String key) {
		return ActionContext.getContext().getSession().get(key);
	}

	public void setSession(String key, Object value) {
		ActionContext.getContext().getSession().put(key, value);
	}

	public void setSessionFlag(User user) {
		logger.debug("set session flag for user[name:" + user.getEmail() + "]");
		/* add user id in sesion */
		setSession(Constants.USER_ID, user.getId());
		/* add user email in sesion as account */
		setSession(Constants.ACCOUNT, user.getEmail());
		setSession(Constants.PASSWORD, user.getPassword());
		if (AuthorityContext.getSuperUser().equals(user)) {
			setSession(Constants.MANAGEABLE, true);
		}
	}

	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}

	public boolean checkLogin(String username, String password) {
		logger.debug("check user login[username:" + username + ",password:" + password + "]");
		User user = userManager.getUserByAccount(username);
		return user.authorize(password);
	}

	public void setRegionManager(IRegionManager regionManager) {
		this.regionManager = regionManager;
	}

}
